<?php
/**
 * Created by PhpStorm.
 * User: ari
 * Date: 11/19/14
 * Time: 11:30 AM
 */
namespace BC\User\Commands;

use BC\PGP\Commands\PGPGenerateKeyPairCommand;
use BC\PGP\PGPConfig;
use BC\User\User;
use CPath\Data\Map\IKeyMapper;
use CPath\Request\IRequest;

class CreateUser extends PGPGenerateKeyPairCommand
{
	function __construct($userID, $email=null, $keyLength=null) {
		parent::__construct($userID, array(
			self::ARG_EMAIL => $email,
			self::ARG_KEY_LENGTH => $keyLength,
		));
		$this->setPrimaryKeyRing(PGPConfig::$KEYRING_USER);
	}


	/**
	 * Execute a command and return a response. Does not render
	 * @param IRequest $Request
	 * @param null $argList
	 * @throws \Exception
	 * @return CreateUser
	 */
	function execute(IRequest $Request=null, $argList = null) {
		$t = microtime(true);
		$Response = parent::execute($Request, $argList, true);
		if($Request)
			$Request->log(sprintf("User created: %s in %.2f seconds", $this->getUser()->getUserID(), microtime(true) - $t));
		return $Response;
	}

	function getUser() {
		$ids = $this->getKeyIDs();
		return new User($ids[0]);
	}

	/**
	 * Map data to the key map
	 * @param IKeyMapper $Map the map inst to add data to
	 * @internal param \CPath\Request\IRequest $Request
	 * @internal param \CPath\Request\IRequest $Request
	 * @return void
	 */
	function mapKeys(IKeyMapper $Map) {
		parent::mapKeys($Map);
		$Map->map('user', $this->getUser());
	}


}

